import request from '@/utils/request';
import type { RegulationData } from '@/api/types/regulation';

const prefix = '/api/regulation-services/regulation_services';

export const addRegulationService = (data: RegulationData) =>
    request.post(prefix, data);

export const getRegulationServices = () =>
    request.get<RegulationData[]>(prefix);

export const getRegulationService = (id: number) =>
    request.get<RegulationData>(`${prefix}/${id}`);

export const updateRegulationService = (id: number, data: Partial<RegulationData>) =>
    request.put(`${prefix}/${id}`, data);

export const deleteRegulationService = (id: number) =>
    request.delete(`${prefix}/${id}`);

/**
 * 根据年份、城市、区县、乡镇获取 GEP 计算结果
 * @param year - 年份
 * @param city - 城市名称
 * @param district - 区县名称
 * @param town - 乡镇名称
 * @returns Promise<GepResult>
 */
export const getRegulationServiceByPath = (year: number, city: string, district: string, town: string) => {
    // 检查参数是否为空
    if (!city || !district || !town) {
        console.warn('城市、区县、乡镇参数不能为空');
        return Promise.resolve({} as RegulationData); // 返回空对象或默认值
    }
    return request.get<RegulationData>(`${prefix}_by_fields/${year}/${encodeURIComponent(city)}/${encodeURIComponent(district)}/${encodeURIComponent(town)}`);
}
/**
 * 根据年份、城市、区县、乡镇更新 GEP 计算结果
 * @param year - 年份
 * @param city - 城市名称
 * @param district - 区县名称
 * @param town - 乡镇名称
 * @param data - 需要更新的数据对象
 * @returns Promise<GepResult>
 */
export const updateRegulationServiceByPath = (year: number, city: string, district: string, town: string, data: Partial<RegulationData>) =>
    request.put<RegulationData>(`${prefix}_by_fields/${year}/${encodeURIComponent(city)}/${encodeURIComponent(district)}/${encodeURIComponent(town)}`, data);
